package com.mantou.proxyservice.proxeservice.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;







import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.mantou.proxyservice.proxeservice.entity.Proxy;
import com.mantou.proxyservice.proxeservice.service.ProxyService;
import com.mantou.proxyservice.utils.ReturnCode;
import com.mantou.proxyservice.utils.ReturnContainer;

@Controller
@RequestMapping("/proxy")
public class ProxyRestApiController {

	private final Logger logger = LoggerFactory.getLogger(ProxyRestApiController.class);
	
	
	@Autowired
	private ProxyService proxyService;
	
	@RequestMapping(value = "/aviables/{num}", method = RequestMethod.GET)
	@ResponseBody
	public String avaiable(@PathVariable Integer num){
		List<Proxy> avaibles = proxyService.avibles(num);
		for(Proxy proxy:avaibles){
			proxy.setDirection(null);
			proxy.setId(null);
			proxy.setConnectionlevel(null);
			proxy.setAvailblelevel(null);
			proxy.setTesttimes(null);
		}
		return JSONArray.toJSONString(avaibles);
	}
	
	@RequestMapping(value = "/query/{pagenow}/{pagesize}", method = RequestMethod.GET)
	@ResponseBody
	public String query(@PathVariable Integer pagenow,@PathVariable Integer pagesize){
		List<Proxy> avaibles = proxyService.queryavailable(pagenow,pagesize);
		for(Proxy proxy:avaibles){
			proxy.setDirection(null);
			proxy.setId(null);
			proxy.setConnectionlevel(null);
			proxy.setAvailblelevel(null);
			proxy.setTesttimes(null);
		}
		return JSONArray.toJSONString(avaibles);
	}
	
	@RequestMapping(value = "/proxeservice/proxy/{id}", method = RequestMethod.GET)
	public ResponseEntity<ReturnContainer<Proxy>> getProxyById(@PathVariable Integer id){
		Proxy proxy = proxyService.findByPrimaryKey(id);
		ReturnContainer<Proxy> responseEnv = new ReturnContainer<Proxy>(ReturnCode.OK.getStatus(),proxy);
		return new ResponseEntity<ReturnContainer<Proxy>>(responseEnv, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/proxeservice/proxy", method = RequestMethod.POST)
	public ResponseEntity<ReturnContainer<Integer>> createProxy(@RequestBody Proxy proxy){
		Integer  result = proxyService.create(proxy);
		ReturnContainer<Integer> responseEnv = new ReturnContainer<Integer>(ReturnCode.OK.getStatus(),result);
		return new ResponseEntity<ReturnContainer<Integer>>(responseEnv, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/proxeservice/proxy/{id}", method = RequestMethod.DELETE)
	public ResponseEntity<ReturnContainer<Integer>> deleteProxyByPrimaryKey(@PathVariable Integer id){
		Integer  result = proxyService.deleteByPrimaryKey(id);
		ReturnContainer<Integer> responseEnv = new ReturnContainer<Integer>(ReturnCode.OK.getStatus(),result);
		return new ResponseEntity<ReturnContainer<Integer>>(responseEnv, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/proxeservice/proxy/{id}", method = RequestMethod.PUT)
	public ResponseEntity<ReturnContainer<Integer>> updateProxyByPrimaryKeySelective(@PathVariable Integer id, @RequestBody Proxy proxy){
		proxy.setId(id);
		Integer  result = proxyService.updateByPrimaryKeySelective(proxy);
		ReturnContainer<Integer> responseEnv = new ReturnContainer<Integer>(ReturnCode.OK.getStatus(),result);
		return new ResponseEntity<ReturnContainer<Integer>>(responseEnv, HttpStatus.OK);
	}
	
}
